#!/usr/bin/env python
""" Group Project on Word Alignment

David Coles, Guan Gui, Kai Chen; 2010
$LastChangedBy: guiguan $
"""

__version__ = "$Rev: 174 $"

import codecs
from AlignedSent import AlignedSent
from collections import defaultdict

def load(path_to_giza_file = '../../data/giza/alignment-en-fr',
         encoding='latin1'):
    """This function parses a giza formatted alignment file into a list of 
    AlignedSents
    """
    aligned_sents = []
    
    with codecs.open(path_to_giza_file, 'r', encoding) as raw:     
        while 1:
            cur_line = to_unicode_or_bust(raw.readline(), encoding)
            # break if EOF
            if cur_line == '':
                break
            # skip lines starting with ==> and empty lines
            if cur_line.startswith('==>') or cur_line.rstrip() == '':
                continue
            # 1st sentence
            sent1 = cur_line.split()
            # 2nd sentence
            sent2 = (to_unicode_or_bust(raw.readline(), encoding)).split()
            # the alignments
            # N.B. list comprehensions are more efficient and elegant than 
            # map/lambda combinations
            alignments = [tuple([int(j) for j in i.split('-')]) for i in 
                    (to_unicode_or_bust(raw.readline(), encoding)).split()]
            aligned_sents.append(AlignedSent(sent1, sent2, alignments))
    
    return aligned_sents

def to_unicode_or_bust(obj, encoding='utf-8'):
    """Detects if object is a string and if so converts to unicode, if not 
    already.
    """
    if isinstance(obj, basestring):
        if not isinstance(obj, unicode):
            obj = unicode(obj, encoding)
    return obj
    
if __name__ == '__main__':
    print load('../../data/giza/alignment-en-fr', 'latin_1')
